/*
	       File: ExecuteApplication.cs
	Create Date: 2007-09-05
	    Version:

	Description:


	 Change log:


*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor.Settings;
using GuerillaProgrammer.FileProcessor;

namespace GuerillaProgrammer.FileProcessor.Activities
{
	public partial class ExecuteApplication: Activity
    {
        #region Private member fields
        private string _ApplicationName;
        private string _CommandLine;
        #endregion

        #region Constructors and cleanup methods
        public ExecuteApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        #endregion

        #region Class properties
        public static DependencyProperty ApplicationNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register("ApplicationName", typeof(string), typeof(ExecuteApplication));
        [Description("Full path to the application to be executed.  Can use string macros")]
        [Category("Execute Application")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ApplicationName
        {
            get { return _ApplicationName; }
            set { _ApplicationName = value; }
        }

        public static DependencyProperty CommandLineProperty = System.Workflow.ComponentModel.DependencyProperty.Register("CommandLine", typeof(string), typeof(ExecuteApplication));
        [Description("the command line parameters passed to the application.  Can use string macros")]
        [Category("Execute Application")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string CommandLine
        {
            get { return _CommandLine; }
            set { _CommandLine = value; }
        }

        /*
         //this is here so I can learn how to do property editors for activities.
	    private string _Test;
        public static DependencyProperty TestProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Test", typeof(string), typeof(ExecuteApplication));
        [Description("")]
        [Category("Execute Application")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [EditorAttribute(typeof(string), typeof(GuerillaProgrammer.FileProcessor.Activities.TestPropertyEditor))]
        public string Test
        {
            get { return _CommandLine; }
            set { _CommandLine = value; }
        }
        */
        #endregion

        #region Events handlers
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Logger.LogEnter();
            IProcessorDataService dataService = executionContext.GetService(typeof(IProcessorDataService)) as IProcessorDataService;
            if (null == dataService)
            {
                throw new InvalidOperationException("Can't get the service!");
            }

            WorkItem workItem = dataService.GetWorkItem();
            StringProcessor stringProcessor = new StringProcessor(new FileProcessorSymbolTable(dataService.GetConfiguration(), workItem));

            string processedAppName = stringProcessor.SubstituteStringValues(_ApplicationName);
            string processedArguments = stringProcessor.SubstituteStringValues(_CommandLine);
            string commandLine = string.Format("{0} {1}", processedAppName, processedArguments);

            try
            {
                Logger.LogInfo("Execute program", "Executing : {0}", commandLine);
                Process theApp = Process.Start(processedAppName, processedArguments );
                theApp.WaitForExit();
                
                int exitCode = theApp.ExitCode;
                Logger.LogInfo("Execute program", "exit code: {0}", exitCode);

                Logger.LogLeave();
                return base.Execute(executionContext);
            }
            catch (Exception exc)
            {
                Logger.LogError("Execution error", "An error occured executing the program.\nApplication: {0}\nArguments: {1}\nError: {2}\n", processedAppName, processedArguments, exc);
                throw exc;
            }
        }
        #endregion

        #region Public interface
        #endregion
	}
}
